#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std; 

#define N 351
#define M 121

int s[N]; 
int cnt[5]; 
int n, m; 

/* difinition of aa[a][b][c][d] is the key to solve the question */

vector<vector<vector<vector<int> > > > aa; 

int main()
{
	int i, t; 
	int a, b, c, d; 
	
//	freopen("p1541.in", "r", stdin); 
	
	cin>>n>>m; 
	
	for (i=1; i<=n; i++) {
		cin>>s[i]; 
	}
	
	for (i=1; i<=m; i++) {
		cin>>t; 
		cnt[t]++; 
	}

	aa.resize(cnt[1]+1); 
	for (a=0; a<=cnt[1]; a++) {
		aa[a].resize(cnt[2]+1); 
		for (b=0; b<=cnt[2]; b++) {
			aa[a][b].resize(cnt[3]+1); 
			for (c=0; c<=cnt[3]; c++) {
				aa[a][b][c].resize(cnt[4]+1); 
			}
		}
	}

	aa[0][0][0][0] = s[1]; 
	for (a=0; a<=cnt[1]; a++) {
		for (b=0; b<=cnt[2]; b++) {
			for (c=0; c<=cnt[3]; c++) {
				for (d=0; d<=cnt[4]; d++) {
					int k = 1+a+b*2+c*3+d*4; 
					if (a) 
						aa[a][b][c][d] = max(aa[a][b][c][d], aa[a-1][b][c][d]+s[k]); 
					if (b) 
						aa[a][b][c][d] = max(aa[a][b][c][d], aa[a][b-1][c][d]+s[k]); 
					if (c) 
						aa[a][b][c][d] = max(aa[a][b][c][d], aa[a][b][c-1][d]+s[k]); 
					if (d) 
						aa[a][b][c][d] = max(aa[a][b][c][d], aa[a][b][c][d-1]+s[k]); 
				}
			}
		}
	}
	cout<<aa[cnt[1]][cnt[2]][cnt[3]][cnt[4]]<<endl; 
	return 0; 
}


